<!DOCTYPE html>
<html>
  <head>
  <meta http-equiv="content-type" content="text/html; charset=utf-8">
  <meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport">
  <meta name="description" content="along&#39;s blog">
  <meta name="keyword" content="hexo-theme, vuejs">
  
    <link rel="shortcut icon" href="/css/images/logo.png">
  
  <title>
    
      静态网页生成[gridsome] | ALONG
    
  </title>
  <link href="/cdn.along/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">
  <link href="/cdn.along/nprogress/0.2.0/nprogress.min.css" rel="stylesheet">
  <link href="/cdn.along/highlight.js/9.12.0/styles/tomorrow.min.css" rel="stylesheet">
  
<link rel="stylesheet" href="/css/style.css">

  
  <script src="/cdn.along/jquery/3.2.1/jquery.min.js"></script>
  <script src="/cdn.along/geopattern/1.2.3/js/geopattern.min.js"></script>
  <script src="/cdn.along/nprogress/0.2.0/nprogress.min.js"></script>
  
    
<script src="/js/qrious.js"></script>

  
  
  
  
    <!-- MathJax support START -->
    <script type="text/x-mathjax-config">
      MathJax.Hub.Config({
        tex2jax: {
          inlineMath: [ ['$','$'], ["\\(","\\)"]  ],
          processEscapes: true,
          skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
        }
      });
    </script>

    <script type="text/x-mathjax-config">
      MathJax.Hub.Queue(function() {
        var all = MathJax.Hub.getAllJax(), i;
        for (i=0; i < all.length; i += 1) {
          all[i].SourceElement().parentNode.className += ' has-jax';
        }
      });
    </script>
    <script type="text/javascript" src="/cdn.along/mathjax/2.7.5/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
    <!-- MathJax support END -->
  


  
  
    
<script src="/js/local-search.js"></script>


<meta name="generator" content="Hexo 5.4.0"></head>
<div class="wechat-share">
  <img src="/css/images/logo.png" />
</div>
  <body>
    <header class="header fixed-header">
  <div class="header-container">
    <a class="home-link" href="/">
      <div class="logo"></div>
      <span>ALONG</span>
    </a>
    <ul class="right-list">
      
        <li class="list-item">
          
            <a href="/" class="item-link">Home</a>
          
        </li>
      
        <li class="list-item">
          
            <a href="/tags/" class="item-link">Tags</a>
          
        </li>
      
        <li class="list-item">
          
            <a href="/archives/" class="item-link">Archives</a>
          
        </li>
      
        <li class="list-item">
          
            <a href="/about/" class="item-link">About</a>
          
        </li>
      
      
        <li class="menu-item menu-item-search right-list">
    <a role="button" class="popup-trigger">
        <i class="fa fa-search fa-fw"></i>
    </a>
</li>
      
    </ul>
    <div class="menu">
      <span class="icon-bar"></span>
      <span class="icon-bar"></span>
      <span class="icon-bar"></span>
    </div>
    <div class="menu-mask">
      <ul class="menu-list">
        
          <li class="menu-item">
            
              <a href="/" class="menu-link">Home</a>
            
          </li>
        
          <li class="menu-item">
            
              <a href="/tags/" class="menu-link">Tags</a>
            
          </li>
        
          <li class="menu-item">
            
              <a href="/archives/" class="menu-link">Archives</a>
            
          </li>
        
          <li class="menu-item">
            
              <a href="/about/" class="menu-link">About</a>
            
          </li>
        
      </ul>
    </div>
    
      <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
            <span class="search-icon">
                <i class="fa fa-search"></i>
            </span>
            <div class="search-input-container">
                <input autocomplete="off" autocapitalize="off"
                    placeholder="Please enter your keyword(s) to search." spellcheck="false"
                    type="search" class="search-input">
            </div>
            <span class="popup-btn-close">
                <i class="fa fa-times-circle"></i>
            </span>
        </div>
        <div id="search-result">
            <div id="no-result">
                <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
            </div>
        </div>
    </div>
</div>
    
  </div>
</header>

    <div id="article-banner">
  <h2>静态网页生成[gridsome]</h2>
  <p class="post-date">2020-09-29</p>
  <div class="arrow-down">
    <a href="javascript:;"></a>
  </div>
</div>
<main class="app-body flex-box">
  <!-- Article START -->
  <article class="post-article">
    <section class="markdown-content"><h2 id="静态网站"><a href="#静态网站" class="headerlink" title="静态网站"></a>静态网站</h2><h3 id="什么是静态网站"><a href="#什么是静态网站" class="headerlink" title="什么是静态网站"></a>什么是静态网站</h3><ul>
<li>静态网站是使用一系列模板数据及配置生成静态html文件及相关资源的工具</li>
<li>这个功能也叫作预渲染</li>
<li>生成的网站不需要类似PHP这样的服务器</li>
<li>只需要放在web server或者cdn上就可以</li>
</ul>
<h3 id="静态网站的优点"><a href="#静态网站的优点" class="headerlink" title="静态网站的优点"></a>静态网站的优点</h3><ul>
<li>不需要专业服务器，服务器成本低，只需要能托管静态文件的空间</li>
<li>响应快速，不经过后台处理，只传输内容</li>
<li>安全性更高</li>
</ul>
<h3 id="常见静态网站生成器（JAMStack-Javascript-Api-Markup-stack）"><a href="#常见静态网站生成器（JAMStack-Javascript-Api-Markup-stack）" class="headerlink" title="常见静态网站生成器（JAMStack Javascript Api Markup stack）"></a>常见静态网站生成器（JAMStack Javascript Api Markup stack）</h3><ul>
<li>jekyll(ruby)</li>
<li>hexo(node)</li>
<li>hugo(golang)</li>
<li>gatsby(node/react)</li>
<li>gridsome(node/vue)</li>
<li>next,nuxt也能用于生成静态网站，不过更多的被认为是ssr框架</li>
</ul>
<h3 id="不适用场景"><a href="#不适用场景" class="headerlink" title="不适用场景"></a>不适用场景</h3><ul>
<li>不适合大量路由的应用</li>
<li>不适合大量动态交互的应用，比如后台管理</li>
</ul>
<h2 id="Gridsome"><a href="#Gridsome" class="headerlink" title="Gridsome"></a>Gridsome</h2><ul>
<li>GitHub 仓库：<a href="https://github.com/gridsome/gridsome">https://github.com/gridsome/gridsome</a></li>
<li>官网：<a target="_blank" rel="noopener" href="https://gridsome.org/">https://gridsome.org/</a></li>
</ul>
<h3 id="起步"><a href="#起步" class="headerlink" title="起步"></a>起步</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm install -g @gridsome&#x2F;cli</span><br></pre></td></tr></table></figure>

<p>直接使用<code>gridsome create 项目名</code>创建项目会失败，因为geidsom依赖了<a href="https://github.com/lovell/sharp">sharp</a>（用于处理图片）第三方模块，包含c++文件，另外sharp依赖libvips，这个模块很大容易下载失败。所以需要处理这个问题</p>
<p>关于解决libvips的网络问题可以参照<a target="_blank" rel="noopener" href="https://sharp.pixelplumbing.com/install#chinese-mirror">这里</a>,libvips官网提供了国内的镜像</p>
<p>这里我们使用两个命令配置下镜像源</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">npm config set sharp_binary_host &quot;https:&#x2F;&#x2F;npm.taobao.org&#x2F;mirrors&#x2F;sharp&quot;</span><br><span class="line"></span><br><span class="line">npm config set sharp_libvips_binary_host &quot;https:&#x2F;&#x2F;npm.taobao.org&#x2F;mirrors&#x2F;sharp-libvips&quot;</span><br></pre></td></tr></table></figure>

<p>关于编译c++扩展包可以借助<code>node-gyp</code>模块</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm install node-gyp -g</span><br></pre></td></tr></table></figure>

<p>安装了node-gyp之后不能直接使用，windows环境下需要安装python，另外需要安装windows-build-tools<br>安装时需要使用管理员命令行才能成功</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm install --global --production windows-build-tools</span><br></pre></td></tr></table></figure>

<p>创建项目</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">gridsome create gridsome-site</span><br></pre></td></tr></table></figure>

<p>启动项目</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm run develop</span><br></pre></td></tr></table></figure>

<p>此时会在默认8080端口访问gridsome创建的默认应用</p>
<h3 id="预渲染"><a href="#预渲染" class="headerlink" title="预渲染"></a>预渲染</h3><p>在创建项目的src/pages中可以看到默认生成的两个路由组件，执行打包<code>npm run build</code>查看生成的路由静态页面</p>
<p>可以看到项目中生成了dist目录，保存了所有的静态页面，dist目录下的项目可以直接放在静态web服务上访问</p>
<p>这样生成的网站首次访问时属于服务端渲染（这个过程保存结果在静态资源里，称为预渲染）。到达客户端之后，用户拿到的是一个SPA的应用。</p>
<h3 id="项目配置"><a href="#项目配置" class="headerlink" title="项目配置"></a>项目配置</h3><p>gridsome.config.js 是项目的配置信息，比如标题<code>siteName</code>、路径前缀<code>pathPrefix</code>等，可以参照<a target="_blank" rel="noopener" href="https://gridsome.org/docs/config/">gridsome配置</a></p>
<h3 id="pages路由规则"><a href="#pages路由规则" class="headerlink" title="pages路由规则"></a>pages路由规则</h3><p>pages目录下保存着项目的路由文件，默认会根据文件系统自动生成路由</p>
<h4 id="文件系统路由生成的规则"><a href="#文件系统路由生成的规则" class="headerlink" title="文件系统路由生成的规则"></a>文件系统路由生成的规则</h4><ul>
<li>src/pages/Index.vue -&gt; /</li>
<li>src/pages/AboutUs.vue -&gt; /about-us/</li>
<li>src/pages/about/Vision.vue -&gt; /about/vision/</li>
<li>src/pages/blog/Index.vue -&gt; /blog/</li>
</ul>
<h4 id="基于文件系统的动态路由"><a href="#基于文件系统的动态路由" class="headerlink" title="基于文件系统的动态路由"></a>基于文件系统的动态路由</h4><ul>
<li>src/pages/user/[id].vue -&gt; /user/:id.</li>
<li>src/pages/user/[id]/settings.vue -&gt; /user/:id/settings.</li>
</ul>
<p>在组建中通过<code>$route.params.id</code>获取动态路由的参数</p>
<p>Ex: </p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">&lt;template&gt;</span><br><span class="line">    &lt;div <span class="class"><span class="keyword">class</span></span>=<span class="string">&#x27;user-page&#x27;</span>&gt;</span><br><span class="line">        &#123;&#123; $route.params.id &#125;&#125;</span><br><span class="line">    &lt;/div&gt;</span><br><span class="line">&lt;/template&gt;</span><br></pre></td></tr></table></figure>

<h4 id="api编程方式生成路由"><a href="#api编程方式生成路由" class="headerlink" title="api编程方式生成路由"></a>api编程方式生成路由</h4><p>有时候需要通过api编程方式创建组件</p>
<p>在gridsome.server.js中</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">api.createPages(<span class="function">(<span class="params">&#123; createPage &#125;</span>) =&gt;</span> &#123;</span><br><span class="line">    createPage(&#123;</span><br><span class="line">      path: <span class="string">&#x27;/my-page&#x27;</span>,</span><br><span class="line">      component: <span class="string">&#x27;./src/templates/MyPage.vue&#x27;</span></span><br><span class="line">    &#125;)</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>

<p>在src/templates中创建MyPage.vue</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">&lt;template&gt;</span><br><span class="line">    &lt;div <span class="class"><span class="keyword">class</span></span>=<span class="string">&#x27;my-page&#x27;</span>&gt;My Page&lt;/div&gt;</span><br><span class="line">&lt;/template&gt;</span><br></pre></td></tr></table></figure>

<p>修改了<code>gridsome.server.js</code>需要使用<code>npm run develop</code> 重新启动服务</p>
<p>启动成功后访问/my-page查看刚才创建的页面</p>
<h4 id="api编程方式动态路由"><a href="#api编程方式动态路由" class="headerlink" title="api编程方式动态路由"></a>api编程方式动态路由</h4><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">api.createPages(<span class="function">(<span class="params">&#123; createPage &#125;</span>) =&gt;</span> &#123;</span><br><span class="line">    createPage(&#123;</span><br><span class="line">      path: <span class="string">&#x27;/article/:id(\\d+)&#x27;</span>,  <span class="comment">// () 中是使用正则表达式对id做的限定，这里代表id必须为数字,如果不匹配会404</span></span><br><span class="line">      component: <span class="string">&#x27;./src/templates/Article.vue&#x27;</span></span><br><span class="line">    &#125;)</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>
<p>./src/templates/Article.vue</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">&lt;template&gt;</span><br><span class="line">    &lt;div <span class="class"><span class="keyword">class</span></span>=<span class="string">&#x27;article&#x27;</span>&gt;</span><br><span class="line">        &#123;&#123;$route.params.id&#125;&#125;</span><br><span class="line">    &lt;/div&gt;</span><br><span class="line">&lt;/template&gt;</span><br></pre></td></tr></table></figure>

<p>同样修改gridsome.server.js后需要重启</p>
<h3 id="页面head内容"><a href="#页面head内容" class="headerlink" title="页面head内容"></a>页面head内容</h3><p>使用metaInfo配置页面head中的内容，包括title，meta等</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">  metaInfo: &#123;</span><br><span class="line">    title: <span class="string">&#x27;About us&#x27;</span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="404页面"><a href="#404页面" class="headerlink" title="404页面"></a>404页面</h3><p>自定义404页面只需要在src/pages下创建404.vue即可</p>
<h3 id="集合"><a href="#集合" class="headerlink" title="集合"></a>集合</h3><p>集合用于承载接口的数据，结合模板预渲染成一个一个的页面，更多参展<a target="_blank" rel="noopener" href="https://gridsome.org/docs/collections/">集合</a></p>
<h4 id="数据预取，保存到集合中"><a href="#数据预取，保存到集合中" class="headerlink" title="数据预取，保存到集合中"></a>数据预取，保存到集合中</h4><p>安装<code>axios</code></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm i axios </span><br></pre></td></tr></table></figure>

<p>gridsome.server.js</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">api.loadSource(<span class="keyword">async</span> (actions) =&gt; &#123;</span><br><span class="line">    <span class="comment">// Use the Data Store API here: https://gridsome.org/docs/data-store-api/</span></span><br><span class="line">    </span><br><span class="line">    <span class="keyword">const</span> collection = actions.addCollection(<span class="string">&#x27;Post&#x27;</span>) <span class="comment">// 集合名称</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">const</span> &#123; data &#125; = <span class="keyword">await</span> axios.get(<span class="string">&#x27;https://jsonplaceholder.typicode.com/posts&#x27;</span>)</span><br><span class="line"></span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">const</span> item <span class="keyword">of</span> data) &#123;</span><br><span class="line">        collection.addNode(&#123;</span><br><span class="line">            id: item.id,</span><br><span class="line">            title: item.title,</span><br><span class="line">            content: item.body</span><br><span class="line">        &#125;)</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>

<p>这样，集合就预取保存了接口的数据，接线来得问题是如何在页面中获取这个数据</p>
<p>集合会将数据保存到GraphQL中，上述代码中通过<code>actions.addCollection(&#39;Post&#39;)</code>我们添加了集合并命名为<code>Post</code>，此时在GraphQL中</p>
<ul>
<li><code>post</code> 获取单条数据 通过 <code>id</code></li>
<li><code>allPost</code> 获取数据列表</li>
</ul>
<p>我们可以通过访问启动项目是给出的页面查看GraphQL内容</p>
<img src="/2020/09/29/Gridsome-%E9%9D%99%E6%80%81%E7%BD%91%E9%A1%B5%E7%94%9F%E6%88%90/1.png" class="">

<p>访问 <code>http://localhost:8080/___explore</code></p>
<img src="/2020/09/29/Gridsome-%E9%9D%99%E6%80%81%E7%BD%91%E9%A1%B5%E7%94%9F%E6%88%90/2.png" class="">

<p>点击docs可以查看所有内容<br>尝试查询一条数据，</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">query&#123;</span><br><span class="line">  post (id: 1) &#123;</span><br><span class="line">    id</span><br><span class="line">    title</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<img src="/2020/09/29/Gridsome-%E9%9D%99%E6%80%81%E7%BD%91%E9%A1%B5%E7%94%9F%E6%88%90/3.png" class="">

<p>关于GraphQL内容参考<a target="_blank" rel="noopener" href="https://graphql.cn/">GraphQL官网</a></p>
<h4 id="页面获取GraphQL中的数据"><a href="#页面获取GraphQL中的数据" class="headerlink" title="页面获取GraphQL中的数据"></a>页面获取GraphQL中的数据</h4><p><a target="_blank" rel="noopener" href="https://gridsome.org/docs/querying-data/">Querying data页面获取数据参考地址</a></p>
<blockquote>
<p>You can query data from the GraphQL data layer into any Page, Template or Component. Queries are added with a <page-query> or <static-query> block in Vue Components.</p>
<ul>
<li>Use <page-query> in Pages &amp; Templates.</li>
<li>Use <static-query> in Components.</li>
</ul>
</blockquote>
<ul>
<li>在templates或Pages中的组件通过<code>&lt;page-query&gt;</code></li>
<li>在component中使用<code>&lt;static-query&gt;</code></li>
</ul>
<p>接下来尝试使用数据并预渲染界面</p>
<p>创建src/pages/Posts.vue</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line">&lt;template&gt;</span><br><span class="line">    &lt;Layout&gt;</span><br><span class="line">        &lt;div <span class="class"><span class="keyword">class</span></span>=<span class="string">&#x27;posts&#x27;</span>&gt;</span><br><span class="line">            &lt;h1&gt;Posts&lt;/h1&gt;</span><br><span class="line">            &lt;ul&gt;</span><br><span class="line">                &lt;li v-<span class="keyword">for</span>=<span class="string">&quot;edge in $page.posts.edges&quot;</span> :key=<span class="string">&quot;edge.node.id&quot;</span>&gt;</span><br><span class="line">                    &lt;g-link to=<span class="string">&quot;/&quot;</span>&gt;&#123;&#123;edge.node.title&#125;&#125;&lt;/g-link&gt;</span><br><span class="line">                &lt;/li&gt;</span><br><span class="line">            &lt;/ul&gt;</span><br><span class="line">        &lt;/div&gt;</span><br><span class="line">    &lt;/Layout&gt;</span><br><span class="line">&lt;/template&gt;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">&lt;page-query&gt;</span><br><span class="line">query &#123;</span><br><span class="line">  posts: allPost &#123;</span><br><span class="line">    edges &#123;</span><br><span class="line">      node &#123;</span><br><span class="line">        id</span><br><span class="line">        title</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line">&lt;/page-query&gt;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>此时可以访问到静态化的posts页面</p>
<img src="/2020/09/29/Gridsome-%E9%9D%99%E6%80%81%E7%BD%91%E9%A1%B5%E7%94%9F%E6%88%90/4.png" class="">

<h4 id="静态或文章详情页面"><a href="#静态或文章详情页面" class="headerlink" title="静态或文章详情页面"></a>静态或文章详情页面</h4><ul>
<li>配置gridsome.config.js,指定模板和路由规则</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">module</span>.exports = &#123;</span><br><span class="line">  ...</span><br><span class="line">  templates: &#123;</span><br><span class="line">    <span class="comment">// Post 是集合名称</span></span><br><span class="line">    Post: [&#123;</span><br><span class="line">      path: <span class="string">&#x27;/posts/:id&#x27;</span>, <span class="comment">// 这里动态路由的id就是我们数据中心的唯一字段，不能随便写</span></span><br><span class="line">      component: <span class="string">&#x27;./src/templates/Post.vue&#x27;</span>,</span><br><span class="line">    &#125;]</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li>修改文章列表的链接</li>
</ul>
<p>之前在posts.vue中文章链接修改</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// &lt;g-link to=&quot;/&quot;&gt;&#123;&#123;edge.node.title&#125;&#125;&lt;/g-link&gt;</span></span><br><span class="line"><span class="comment">// edge.node.path是我们配置gridsome.config.js之后回为列表数据添加path属性</span></span><br><span class="line">&lt;g-link :to=<span class="string">&quot;edge.node.path&quot;</span>&gt;&#123;&#123;edge.node.title&#125;&#125;&lt;/g-link&gt;</span><br></pre></td></tr></table></figure>

<ul>
<li>创建模板sre/templates/Page.vue</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line">&lt;template&gt;</span><br><span class="line">    &lt;Layout&gt;</span><br><span class="line">        &lt;div <span class="class"><span class="keyword">class</span></span>=<span class="string">&#x27;post&#x27;</span>&gt;</span><br><span class="line">            &lt;h1&gt;&#123;&#123; $page.post.title &#125;&#125;&lt;/h1&gt;</span><br><span class="line">            &lt;div&gt;</span><br><span class="line">                &#123;&#123; $page.post.content &#125;&#125;</span><br><span class="line">            &lt;/div&gt;</span><br><span class="line">            </span><br><span class="line">        &lt;/div&gt;</span><br><span class="line">    &lt;/Layout&gt;</span><br><span class="line">    </span><br><span class="line">&lt;/template&gt;</span><br><span class="line"></span><br><span class="line">&lt;page-query&gt;</span><br><span class="line">query ($id: ID!)&#123;</span><br><span class="line">  post (id: $id) &#123;</span><br><span class="line">    id</span><br><span class="line">    title</span><br><span class="line">    content</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line">&lt;/page-query&gt;</span><br><span class="line"></span><br><span class="line">&lt;script&gt;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">    name: <span class="string">&#x27;PostPage&#x27;</span>,</span><br><span class="line">    <span class="comment">// 这种写法无法访问 $page中的数据</span></span><br><span class="line">    <span class="comment">// metaInfo: &#123;</span></span><br><span class="line">    <span class="comment">//     title: $page.post.title</span></span><br><span class="line">    <span class="comment">// &#125;</span></span><br><span class="line">    metaInfo() &#123;</span><br><span class="line">        <span class="keyword">return</span> &#123;</span><br><span class="line">            title: <span class="built_in">this</span>.$page.post.title </span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">&lt;/script&gt;</span><br></pre></td></tr></table></figure>

<p><code>query ($id: ID!)</code> 中传入了变量<code>$id</code>, 变量类型是<code>ID</code>(这个类型可以通过docs里查看),<code>!</code>代表不能为空</p>
<p>接下来执行打包 </p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm run build</span><br></pre></td></tr></table></figure>

<p>通过打包后的dist目录可以看到项目中每篇文章都做了静态化处理</p>
<img src="/2020/09/29/Gridsome-%E9%9D%99%E6%80%81%E7%BD%91%E9%A1%B5%E7%94%9F%E6%88%90/5.png" class="">


<h4 id="处理分页"><a href="#处理分页" class="headerlink" title="处理分页"></a>处理分页</h4><p>处理分页很简单，gridsome提供了Pager组件，引入并提供 需要的属性即可<br>在列表页查询列表时需要传递page相关的参数，每页条数，以及当前页</p>
<p>src/page/Posts.vue</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br></pre></td><td class="code"><pre><span class="line">&lt;template&gt;</span><br><span class="line">    &lt;Layout&gt;</span><br><span class="line">        &lt;div <span class="class"><span class="keyword">class</span></span>=<span class="string">&#x27;posts&#x27;</span>&gt;</span><br><span class="line">            &lt;h1&gt;Posts&lt;/h1&gt;</span><br><span class="line">            &lt;ul&gt;</span><br><span class="line">                &lt;li v-<span class="keyword">for</span>=<span class="string">&quot;edge in $page.posts.edges&quot;</span> :key=<span class="string">&quot;edge.node.id&quot;</span>&gt;</span><br><span class="line">                    &lt;!-- edge.node.path是自动生成的属性，可以跳转到详情页 --&gt;</span><br><span class="line">                    &lt;g-link :to=<span class="string">&quot;edge.node.path&quot;</span>&gt;&#123;&#123;edge.node.title&#125;&#125;&lt;/g-link&gt;</span><br><span class="line">                &lt;/li&gt;</span><br><span class="line">            &lt;/ul&gt;</span><br><span class="line">            &lt;!-- 使用pager组件 --&gt;</span><br><span class="line">            &lt;pager :info=<span class="string">&quot;$page.posts.pageInfo&quot;</span>/&gt;</span><br><span class="line">        &lt;/div&gt;</span><br><span class="line">    &lt;/Layout&gt;</span><br><span class="line">&lt;/template&gt;</span><br><span class="line">&lt;page-query&gt;</span><br><span class="line"># 接收url上的参数，设置每页条数和当前页码</span><br><span class="line">query ($page: Int)&#123;</span><br><span class="line">  posts: allPost (perPage: <span class="number">10</span>, <span class="attr">page</span>: $page) @paginate &#123;</span><br><span class="line">    pageInfo &#123;</span><br><span class="line">      totalPages</span><br><span class="line">      currentPage</span><br><span class="line">    &#125;</span><br><span class="line">    edges &#123;</span><br><span class="line">      node &#123;</span><br><span class="line">        id</span><br><span class="line">        title</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line">&lt;/page-query&gt;</span><br><span class="line"></span><br><span class="line">&lt;script&gt;</span><br><span class="line"><span class="comment">// 引入Pager</span></span><br><span class="line"><span class="keyword">import</span> &#123;Pager&#125; <span class="keyword">from</span> <span class="string">&#x27;gridsome&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">    <span class="comment">// 注册</span></span><br><span class="line">    components: &#123;Pager&#125;,</span><br><span class="line">    ...</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>打开网页查看，点击第二页，可以看到路由变化为<code>localhost:8080/posts/2/</code></p>
<img src="/2020/09/29/Gridsome-%E9%9D%99%E6%80%81%E7%BD%91%E9%A1%B5%E7%94%9F%E6%88%90/6.png" class="">

<p>至于paper的样式自定义即可</p>
</section>
    <!-- Tags START -->
    
      <div class="tags">
        <span>Tags:</span>
        
  <a href="/tags#Gridsome" >
    <span class="tag-code">Gridsome</span>
  </a>

      </div>
    
    <!-- Tags END -->
    <!-- NAV START -->
    
  <div class="nav-container">
    <!-- reverse left and right to put prev and next in a more logic postition -->
    
      <a class="nav-left" href="/2020/09/27/%E6%90%AD%E5%BB%BAVue-SSR/">
        <span class="nav-arrow">← </span>
        
          搭建Vue SSR
        
      </a>
    
    
      <a class="nav-right" href="/2020/10/23/%E5%8E%9F%E5%9E%8B%E9%93%BE%E7%BB%A7%E6%89%BF/">
        
          原型链继承
        
        <span class="nav-arrow"> →</span>
      </a>
    
  </div>

    <!-- NAV END -->
    <!-- 打赏 START -->
    
      <div class="money-like">
        <div class="reward-btn">
          赏
          <span class="money-code">
            <span class="alipay-code">
              <div class="code-image"></div>
              <b>使用支付宝打赏</b>
            </span>
            <span class="wechat-code">
              <div class="code-image"></div>
              <b>使用微信打赏</b>
            </span>
          </span>
        </div>
        <p class="notice">若你觉得我的文章对你有帮助，欢迎点击上方按钮对我打赏</p>
      </div>
    
    <!-- 打赏 END -->
    <!-- 二维码 START -->
    
      <div class="qrcode">
        <canvas id="share-qrcode"></canvas>
        <p class="notice">扫描二维码，分享此文章</p>
      </div>
    
    <!-- 二维码 END -->
    
      <!-- No Comment -->
    
  </article>
  <!-- Article END -->
  <!-- Catalog START -->
  
    <aside class="catalog-container">
  <div class="toc-main">
    <strong class="toc-title">Catalog</strong>
    
      <ol class="toc-nav"><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#%E9%9D%99%E6%80%81%E7%BD%91%E7%AB%99"><span class="toc-nav-text">静态网站</span></a><ol class="toc-nav-child"><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#%E4%BB%80%E4%B9%88%E6%98%AF%E9%9D%99%E6%80%81%E7%BD%91%E7%AB%99"><span class="toc-nav-text">什么是静态网站</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#%E9%9D%99%E6%80%81%E7%BD%91%E7%AB%99%E7%9A%84%E4%BC%98%E7%82%B9"><span class="toc-nav-text">静态网站的优点</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#%E5%B8%B8%E8%A7%81%E9%9D%99%E6%80%81%E7%BD%91%E7%AB%99%E7%94%9F%E6%88%90%E5%99%A8%EF%BC%88JAMStack-Javascript-Api-Markup-stack%EF%BC%89"><span class="toc-nav-text">常见静态网站生成器（JAMStack Javascript Api Markup stack）</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#%E4%B8%8D%E9%80%82%E7%94%A8%E5%9C%BA%E6%99%AF"><span class="toc-nav-text">不适用场景</span></a></li></ol></li><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#Gridsome"><span class="toc-nav-text">Gridsome</span></a><ol class="toc-nav-child"><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#%E8%B5%B7%E6%AD%A5"><span class="toc-nav-text">起步</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#%E9%A2%84%E6%B8%B2%E6%9F%93"><span class="toc-nav-text">预渲染</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#%E9%A1%B9%E7%9B%AE%E9%85%8D%E7%BD%AE"><span class="toc-nav-text">项目配置</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#pages%E8%B7%AF%E7%94%B1%E8%A7%84%E5%88%99"><span class="toc-nav-text">pages路由规则</span></a><ol class="toc-nav-child"><li class="toc-nav-item toc-nav-level-4"><a class="toc-nav-link" href="#%E6%96%87%E4%BB%B6%E7%B3%BB%E7%BB%9F%E8%B7%AF%E7%94%B1%E7%94%9F%E6%88%90%E7%9A%84%E8%A7%84%E5%88%99"><span class="toc-nav-text">文件系统路由生成的规则</span></a></li><li class="toc-nav-item toc-nav-level-4"><a class="toc-nav-link" href="#%E5%9F%BA%E4%BA%8E%E6%96%87%E4%BB%B6%E7%B3%BB%E7%BB%9F%E7%9A%84%E5%8A%A8%E6%80%81%E8%B7%AF%E7%94%B1"><span class="toc-nav-text">基于文件系统的动态路由</span></a></li><li class="toc-nav-item toc-nav-level-4"><a class="toc-nav-link" href="#api%E7%BC%96%E7%A8%8B%E6%96%B9%E5%BC%8F%E7%94%9F%E6%88%90%E8%B7%AF%E7%94%B1"><span class="toc-nav-text">api编程方式生成路由</span></a></li><li class="toc-nav-item toc-nav-level-4"><a class="toc-nav-link" href="#api%E7%BC%96%E7%A8%8B%E6%96%B9%E5%BC%8F%E5%8A%A8%E6%80%81%E8%B7%AF%E7%94%B1"><span class="toc-nav-text">api编程方式动态路由</span></a></li></ol></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#%E9%A1%B5%E9%9D%A2head%E5%86%85%E5%AE%B9"><span class="toc-nav-text">页面head内容</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#404%E9%A1%B5%E9%9D%A2"><span class="toc-nav-text">404页面</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#%E9%9B%86%E5%90%88"><span class="toc-nav-text">集合</span></a><ol class="toc-nav-child"><li class="toc-nav-item toc-nav-level-4"><a class="toc-nav-link" href="#%E6%95%B0%E6%8D%AE%E9%A2%84%E5%8F%96%EF%BC%8C%E4%BF%9D%E5%AD%98%E5%88%B0%E9%9B%86%E5%90%88%E4%B8%AD"><span class="toc-nav-text">数据预取，保存到集合中</span></a></li><li class="toc-nav-item toc-nav-level-4"><a class="toc-nav-link" href="#%E9%A1%B5%E9%9D%A2%E8%8E%B7%E5%8F%96GraphQL%E4%B8%AD%E7%9A%84%E6%95%B0%E6%8D%AE"><span class="toc-nav-text">页面获取GraphQL中的数据</span></a></li><li class="toc-nav-item toc-nav-level-4"><a class="toc-nav-link" href="#%E9%9D%99%E6%80%81%E6%88%96%E6%96%87%E7%AB%A0%E8%AF%A6%E6%83%85%E9%A1%B5%E9%9D%A2"><span class="toc-nav-text">静态或文章详情页面</span></a></li><li class="toc-nav-item toc-nav-level-4"><a class="toc-nav-link" href="#%E5%A4%84%E7%90%86%E5%88%86%E9%A1%B5"><span class="toc-nav-text">处理分页</span></a></li></ol></li></ol></li></ol>
    
  </div>
</aside>
  
  <!-- Catalog END -->
</main>

<script>
  (function () {
    var url = 'https://github.com/alongithub/2020/09/29/Gridsome-静态网页生成/';
    var banner = ''
    if (banner !== '' && banner !== 'undefined' && banner !== 'null') {
      $('#article-banner').css({
        'background-image': 'url(' + banner + ')'
      })
    } else {
      $('#article-banner').geopattern(url)
    }
    $('.header').removeClass('fixed-header')

    // error image
    $(".markdown-content img").on('error', function() {
      $(this).attr('src', '/css/images/error_icon.png')
      $(this).css({
        'cursor': 'default'
      })
    })

    // zoom image
    $(".markdown-content img").on('click', function() {
      var src = $(this).attr('src')
      if (src !== '/css/images/error_icon.png') {
        var imageW = $(this).width()
        var imageH = $(this).height()

        var zoom = ($(window).width() * 0.95 / imageW).toFixed(2)
        zoom = zoom < 1 ? 1 : zoom
        zoom = zoom > 2 ? 2 : zoom
        var transY = (($(window).height() - imageH) / 2).toFixed(2)

        $('body').append('<div class="image-view-wrap"><div class="image-view-inner"><img src="'+ src +'" /></div></div>')
        $('.image-view-wrap').addClass('wrap-active')
        $('.image-view-wrap img').css({
          'width': `${imageW}`,
          'transform': `translate3d(0, ${transY}px, 0) scale3d(${zoom}, ${zoom}, 1)`
        })
        $('html').css('overflow', 'hidden')

        $('.image-view-wrap').on('click', function() {
          $(this).remove()
          $('html').attr('style', '')
        })
      }
    })
  })();
</script>


  <script>
    var qr = new QRious({
      element: document.getElementById('share-qrcode'),
      value: document.location.href
    });
  </script>






    <div class="scroll-top">
  <span class="arrow-icon"></span>
</div>
    <footer class="app-footer">
  <p class="copyright">
    Copyright&copy; 2019-2022 along
    <br>
    <a href="https://beian.miit.gov.cn/" target="_blank">京ICP备2020044791号-1</a>
    <!-- <br>
    Theme by <a href="https://github.com/alongithub">alongithub</a>
     -->
  </p>
</footer>

<script>
  function async(u, c) {
    var d = document, t = 'script',
      o = d.createElement(t),
      s = d.getElementsByTagName(t)[0];
    o.src = u;
    if (c) { o.addEventListener('load', function (e) { c(null, e); }, false); }
    s.parentNode.insertBefore(o, s);
  }
</script>
<script>
  async("/cdn.along/fastclick/1.0.6/fastclick.min.js", function(){
    FastClick.attach(document.body);
  })
</script>

<script>
  var hasLine = 'true';
  async("/cdn.along/highlight.js/9.12.0/highlight.min.js", function(){
    $('figure pre').each(function(i, block) {
      var figure = $(this).parents('figure');
      if (hasLine === 'false') {
        figure.find('.gutter').hide();
      }
      hljs.configure({useBR: true});
      var lang = figure.attr('class').split(' ')[1] || 'code';
      var codeHtml = $(this).html();
      var codeTag = document.createElement('code');
      codeTag.className = lang;
      codeTag.innerHTML = codeHtml;
      $(this).attr('class', '').empty().html(codeTag);
      figure.attr('data-lang', lang.toUpperCase());
      hljs.highlightBlock(block);
    });
  })
</script>
<!-- Baidu Tongji -->


<script src="/js/script.js"></script>


  </body>
</html>